/*
 *Copyright @2015 HanHai
 *
 *All rights reserved.
 */
package com.hanhai.zrb.backend.web.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hanhai.common.annotation.PermissionNeed;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.api.biz.service.UserStatisticService;
import com.hanhai.zrb.api.client.facade.UserStatisticDetailFacade;
import com.hanhai.zrb.api.util.DateUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.UserStatisticDetail;
import com.hanhai.zrb.model.user.dto.UserStatisticDetailDto;
import com.hanhai.zrb.model.usercenter.CommonStatistic;
import com.hanhai.zrb.model.usercenter.IntentionStatistic;



@Controller
@RequestMapping("userStatistic")
public class UserStatisticController extends BaseController {
	
	@Resource
	private  UserStatisticService userStatisticService;
	@Resource
	private UserStatisticDetailService  userStatisticDetailService;
	@Resource
	private UserStatisticDetailFacade userStatisticDetailFacade;
	@Resource
	private UserService userService;
	
	@PermissionNeed("userstatistic")
	@RequestMapping("/showUser")
	public String showUser(String id,HttpServletRequest request,HttpServletResponse response) {
		request.setAttribute("id", id);
		return "userstatistic/showUser";
	}
	
	
	@RequestMapping("/showCollect")
	public String showCollect(String id,HttpServletRequest request,HttpServletResponse response) {
		request.setAttribute("id", id);
		return "userstatistic/indexCollect";
	}
	
	
    //根据资金ID查询关注人
		@RequestMapping("/queryAttentionUser")
		@ResponseBody
		public String queryFundUser(Long id,Integer page,Integer rows ) {
			Map<String, Object> map = Maps.newHashMap();
             page = (null==page)?1:page;
             rows = (null==rows)?20:rows;
		    PageInfo pageInfo =userStatisticService.queryAttentionUser(id,page, rows);
		    if(pageInfo != null){
		    	map.put("rows", pageInfo.getList());
		    	map.put("total", pageInfo.getTotal());
		    }else{
		    	map.put("rows", Lists.newArrayList());
		    	map.put("total", 0);
		    }
			return JSON.toJSONString(map);
		}
	
		
        //获取用户收藏的项目
		@RequestMapping(value="/getCollectProject")
		@ResponseBody
		public String getCollectProjects(HttpServletRequest request,HttpServletResponse response,Long id, Integer page,Integer rows){
			Map<String, Object> map = Maps.newHashMap();
            page = (null==page)?1:page;
            rows = (null==rows)?20:rows;
            User user = userService.findById(id);
	        if(null!=user){
	        	Long uid = user.getUid();
	        	UserStatisticDetailDto userStatisticDetailDto = new UserStatisticDetailDto();
	        	userStatisticDetailDto.setUserId(uid);
	        	userStatisticDetailDto.setPageNum(page);
	        	userStatisticDetailDto.setPageSize(rows);
	        	userStatisticDetailDto.setType(UserStatisticDetail.Type.pro_col.getCode());
	        	PageInfo pageInfo = userStatisticDetailService.getUserCollectionProjectPage(userStatisticDetailDto);
			    if(pageInfo != null){
			    	map.put("rows", pageInfo.getList());
			    	map.put("total", pageInfo.getTotal());
			    }else{
			    	map.put("rows", Lists.newArrayList());
			    	map.put("total", 0);
			    }
	        }
	        return JSON.toJSONString(map);
		}
		
        //获取用户收藏的意向
		@RequestMapping(value="/getCollectIntention")
		@ResponseBody
		public String getCollectIntention(HttpServletRequest request,HttpServletResponse response,Long id, Integer page,Integer rows){
			Map<String, Object> map = Maps.newHashMap();
            page = (null==page)?1:page;
            rows = (null==rows)?20:rows;
            User user = userService.findById(id);
	        if(null!=user){
	        	Long uid = user.getUid();
	        	UserStatisticDetailDto userStatisticDetailDto = new UserStatisticDetailDto();
	        	userStatisticDetailDto.setUserId(uid);
	        	userStatisticDetailDto.setPageNum(page);
	        	userStatisticDetailDto.setPageSize(rows);
	        	userStatisticDetailDto.setType(UserStatisticDetail.Type.inten_col.getCode());
	        	PageInfo pageInfo = userStatisticDetailService.getUserCollectionIntentionPage(userStatisticDetailDto);
			    if(pageInfo != null){
			    	map.put("rows", pageInfo.getList());
			    	map.put("total", pageInfo.getTotal());
			    }else{
			    	map.put("rows", Lists.newArrayList());
			    	map.put("total", 0);
			    }
	        }
	        return JSON.toJSONString(map);
		}
		
        //获取用户收藏的资金
		@RequestMapping(value="/getCollectFund")
		@ResponseBody
		public String getCollectFund(HttpServletRequest request,HttpServletResponse response,Long id, Integer page,Integer rows){
			Map<String, Object> map = Maps.newHashMap();
            page = (null==page)?1:page;
            rows = (null==rows)?20:rows;
            User user = userService.findById(id);
	        if(null!=user){
	        	Long uid = user.getUid();
	        	UserStatisticDetailDto userStatisticDetailDto = new UserStatisticDetailDto();
	        	userStatisticDetailDto.setUserId(uid);
	        	userStatisticDetailDto.setPageNum(page);
	        	userStatisticDetailDto.setPageSize(rows);
	        	userStatisticDetailDto.setType(UserStatisticDetail.Type.fund_col.getCode());
	        	PageInfo pageInfo = userStatisticDetailService.getUserCollectionFundPage(userStatisticDetailDto);
			    if(pageInfo != null){
			    	map.put("rows", pageInfo.getList());
			    	map.put("total", pageInfo.getTotal());
			    }else{
			    	map.put("rows", Lists.newArrayList());
			    	map.put("total", 0);
			    }
	        }
	        return JSON.toJSONString(map);
		}

		

}
